package manager

import (
	"fmt"
	"gmap-backend/internal/util/pathutil"
	"os/exec"
)

var cmdPath = pathutil.GetCmdPath()
var explorerPath = pathutil.GetExplorerPath()
var rundll32Path = pathutil.GetRundll32Path()
var photoViewerDll = pathutil.GetPhotoViewerDllPath()

// Cmd_C 以 cmd /c xxx 的方式运行命令
// path 文件路径或命令参数，此处不校验路径有效性，也不用进行格式处理
func Cmd_C(path string, workDir string) *exec.Cmd {
	cmd := exec.Command(cmdPath, "/C", path)
	if 0 < len(workDir) {
		cmd.Dir = workDir
	}
	cmd.Start()
	return cmd
}

func ExplorerOpenDir(path string) {
	exec.Command(explorerPath, path).Start()
}

func ExplorerSelectItem(path string) {
	exec.Command(explorerPath, `/select,`, path).Start()
}

func OpenAs(pathOrUrl string) {
	mainPart := fmt.Sprintf("%s shell32.dll,OpenAs_RunDLL %s", rundll32Path, pathOrUrl)
	Cmd_C(mainPart, "")
}

func CreateOpenByPhotoViewerCmd(path string) string {
	cmdTxt := fmt.Sprintf(`%s "%s", ImageView_Fullscreen %s`, rundll32Path, photoViewerDll, path) +
		"\n" + "exit"
	return cmdTxt
}
